package com.wormhole.wormholemanagement.repository;

import com.wormhole.wormholemanagement.entity.NewsPhoto;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NewsPhotoRepository extends JpaRepository<NewsPhoto, Long>, JpaSpecificationExecutor<NewsPhoto> {
    // 批量更新方法
    @Transactional
    @Modifying
    @Query("UPDATE NewsPhoto p SET p.isMovedToArchive = :isMovedToArchive WHERE p.photoId IN :photoIds")
    int batchUpdateArchiveStatus(List<Long> photoIds, Boolean isMovedToArchive);
    /**
     * 查询 imagePath 包含指定路径的记录
     *
     * @param path 路径关键字
     * @return 包含该路径的记录列表
     */
    @Query("SELECT p FROM NewsPhoto p WHERE p.imagePath LIKE %:path%")
    List<NewsPhoto> findByImagePathContaining(@Param("path") String path);

    @Query("SELECT p FROM NewsPhoto p WHERE p.fileNumber = :fileNumber")
    NewsPhoto findByFileNumber(@Param("fileNumber") String fileNumber);

    @Query("SELECT p FROM NewsPhoto p WHERE p.imageName LIKE  %:imageName%")
    NewsPhoto findByImageName(@Param("imageName") String imageName);

    @Query("SELECT DISTINCT p.imagePath FROM NewsPhoto p")
    List<String> findAllImagePaths();

    /**
     * 根据 imagePath 查询移管图片并按时间升序
     *
     * @param path 图片路径
     * @return 图片列表
     */
    @Query("SELECT p FROM NewsPhoto p WHERE p.imagePath LIKE %:path% AND p.isMovedToArchive = true AND p.fileNumber IS NOT NULL  ORDER BY p.creationDate ASC, p.photoId ASC")
    List<NewsPhoto> findByPath(@Param("path")String path);
}